/**
 * 
 */
package tp8.tsh.cli;

import java.io.File;
import java.io.IOException;

import tp8.tsh.exc.TSHException;

/**
 * @author tpana
 * 
 */
public class CurrentDirectory {

	private File directory;

	/**
	 * @return the directory
	 */
	public synchronized File getDirectory() {
		return directory;
	}

	/**
	 * @param directory the directory to set
	 */
	public synchronized void setDirectory(File directory) {
		this.directory = directory;
	}

	public void changeRelativePath(String relativePath) throws TSHException {
		this.changePath(this.getPath() + this.getSeparator() + relativePath);
	}
	
	public void changePath(String newPath) throws TSHException {

		File newDir = new File(newPath);

		if ((!newDir.exists()) || (!newDir.isDirectory())) {
			throw new TSHException("Directory does not exist: " + newPath);
		}

		this.directory = newDir;
	}
	
	public String getPath() {
		try {
			return directory.getCanonicalPath();
		} catch (IOException e) {
			return "";
		}
	}

	public String getSeparator() {
		return File.separator;
	}
	
	public File[] listAll() {
		return this.directory.listFiles();
	}

	public boolean contains(String filename) {
		return new File(this.getPath()
				+ this.getSeparator() + filename).exists();
	}
	
}
